var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tjimagedefault: 'testimage/timg1.jpg',//默认教师推荐图片
    tjly: 0,
    keyword:'',
    page: 1,
    limit: 20,
    totalpage: 0,
    tjimageUrls: [],//教师推荐
    tjly: 0,

    prompt: {
      hidden: !0,
      icon: '../../assets/images/iconfont-order-default.png',
      title: '暂无相关书籍',
      text: '没有教师推荐的数据，可以看看家长推荐的优秀书籍',
      gurl: '../../../pages/home/home',
      gtype: "switchTab"
    },


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      tjly: options.tjly,
      keyword: options.keyword,
    })
    this.getdata();
  },


  getdata() {
    var that = this;
    var tjly = that.data.tjly;
    var keyword = that.data.keyword;
    var page = that.data.page;
    var limit = that.data.limit;
    if (page != 1) {
      if (page > totalpage) {
        return;
      }
    }
   
    app.wxget({
      url: 'whome/getJstjData.html',
      data: {
        tjly: tjly,
        keyword: keyword,
        page: page,
        limit: limit
      },
      success: function (res) {
        //教师推荐
        var tjs = res.data.list;
        var totalpage = res.data.totalpage;
        var tjimageUrls = that.data.tjimageUrls;
        for (var i = 0; i < tjs.length; i++) {
          tjimageUrls.push(tjs[i]);
        }
        that.setData({
          tjimageUrls: tjimageUrls,
          totalpage: totalpage,
          'prompt.hidden': tjimageUrls.length
        })
      }
    })
  },

  searchdata() {
    this.setData({
      tjimageUrls: [],
      page: 1,
      limit: 10,
      totalpage: 0,
    })
    this.getdata();
  },


  //上拉刷新
  tolower() {
    var that = this;
    var page = parseInt(that.data.page) + 1;
    that.setData({
      page: page
    });
    that.getdata();
  },

  //输入关键字
  keyinput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  errormoretj: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.errorimg
      var tjimageUrls = this.data.tjimageUrls    　　　　　　　//将图片列表数据绑定到变量
      tjimageUrls[errorImgIndex].FM = this.data.tjimagedefault //错误图片替换为默认图片
      this.setData({
        tjimageUrls: tjimageUrls
      })
    }
  },

  //查看书籍详情
  todetail(e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?bid=' + bid
    })
  },


  todetailYxsj(e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../../pages/bookDetail/bookDetail?bid=' + bid
    })
  },

  cancelsearch(){
    wx.redirectTo({
      url: '../../pages/hxtj/hxtj?tjly=0'
    })
  },

})